package module;

import java.util.ArrayList;


import motor.Motor;
import motor.Unite;

public class ModuleStateImpU extends ModuleStateImp
{
	Unite uMere;
	
	private Unite	cible;
	
	public ModuleStateImpU(Unite unite , int type )
	{
		this.uMere = unite;
		this.type = type;
	}
	
	public void goOn( Motor m )
	{
		choix( m );

		actions( m );
	}
	
	
	private void actions(Motor motor)
	{
		switch( getState() )
		{
			case ModuleStateImp.FOLLOW :
			{
				uMere.move(motor);
				break;
			}
			
			case ModuleStateImp.SHOOT :
			{
				uMere.tir(motor);
				break;
			}
			
			case ModuleStateImp.WAIT :
			{
				break;
			}
		}
	}

	private void choix(Motor m)
	{
		if( type == TOURELLE )
		{
			cible = chercherCible( m );
			
			if( cible != null )
			{
				state = SHOOT;
			}
			else
			{
				state = WAIT;
			}
		}
		else if( type == CIVIL )
		{
			state = FOLLOW;
		}
		else if( type == RECOL )
		{
			cible = chercherRessource( m );
			
			if( cible != null )
			{
				state = SHOOT;
			}
			else
			{
				state = WAIT;
			}
		}
	}

	private Unite chercherRessource(Motor m)
	{
		ArrayList<Unite> lu = uMere.getRessources();
		
		Unite nmax = null;
		float max = 0;
		
		for( int i = 0 ; i < lu.size() ; i++ )
		{
			float dis = lu.get(i).distance( uMere.getPos() );
			
			if( dis < uMere.getPortee() )
			{
				if( importance( lu.get(i) ) > max )
				{
					max = importance( lu.get(i) );
					nmax = lu.get(i);
				}
			}
		}
		
		return nmax;
	}

	private Unite chercherCible( Motor m )
	{
		ArrayList<Unite> lu = uMere.getEnnemis();
		
		Unite nmax = null;
		float max = 0;
		
		for( int i = 0 ; i < lu.size() ; i++ )
		{
			float dis = lu.get(i).distance( uMere.getPos() );
			
			if( dis < uMere.getPortee() )
			{
				if( importance( lu.get(i) ) > max )
				{
					max = importance( lu.get(i) );
					nmax = lu.get(i);
				}
			}
		}
		
		return nmax;
	}
	
	public Unite getCible()
	{
		return cible;
	}
	
	private float importance(Unite unite)
	{
		return 10;
	}

}
